<?php
namespace Console\OBD\Events;

use Phalcon\Di\FactoryDefault\Cli as DI;

class Base
{
    protected $_di = null;
    protected $_db = null;
    protected $_logger = null;

    public function initialize()
    {
    }

    /**
     * 获取数据库对象
     * @return \Phalcon\Db\Adapter\Pdo\Mysql
     */
    public function db()
    {
        if ($this->_db == null) {
            $this->_db = $this->getDI()->getShared('db');
        }
        return $this->_db;
    }

    /**
     * @return null|\Phalcon\DiInterface
     */
    public function getDI()
    {
        if ($this->_di == null) {
            $this->_di = (new DI())->getDefault();
        }
        return $this->_di;
    }

    /**
     * 返回日志的操作方法
     * 使用方法
     * $this->logger()->debug('msg')
     * $this->logger()->error('msg')
     * $this->logger()->info('msg')
     * $this->logger()->warning('msg')
     * $this->logger()->notice('msg')
     *
     * @return \Phalcon\Logger\AdapterInterface
     */
    public function logger()
    {
        if ($this->_logger == null) {
            $this->_logger = $this->getDI()->getShared('logger');
        }
        return $this->_logger;
    }

    /**
     * 数组截取转为字符串
     *["28","53","17","07","27","00","03","20","84","00","46","01","00","01","03","07","18","09","25","16","22","41","09","67","11","40","07","70","87","00","00","00","16","00","00","00","00","00","20","01","00","1e","82","1f","40","32","00","32","7d","55","05","78","ff","01","00","02","14","01","00","2e","80","03","01","cc","00","00","27","a8","0e","c5","00","00","00","00","00","00","00","00","00","00","2c","4f","31","29"]
     *
     * @description 截取设备号captureArrayToStr($data,1,6) => 531707270003
     *
     * @param array $data
     * @param       $offset
     * @param       $length
     *
     * @return string
     */
    public function captureArrayToStr(array $data, $offset, $length)
    {
        return implode('', array_slice($data, $offset, $length));
    }

    /**
     * 主要处理硬件返回过来的日期和时间
     * @description
     *             03 07 18=>2018-07-03
     *             09 25 06=>09:25:06
     *             最后合并为2018-07-03 09:25:06
     *
     * @param array $date
     * @param array $time
     *
     * @return false|string
     */
    public function ToDate(array $date, array $time)
    {
        $_date = '20' . implode('-', array_reverse($date));
        $_time = implode(':', $time);
        $date  = date('Y-m-d H:i:s', strtotime($_date . $_time));
        return $date;
    }

    /**
     * 处理位指示
     *
     * @param int $int
     *
     * @return bool|string
     */
    public function formatBinLocationDirection(int $int)
    {
        if ($int > 15 || $int < 0) {
            return false;
        }
        $bin    = decbin($int);
        $format = sprintf('%04d', $bin);
        $data   = [
            0 => $format[0] == 1 ? '定位' : '盲区',
            1 => $format[1] == 1 ? 'N' : 'S',
            2 => $format[2] == 1 ? 'E' : 'W',
            3 => $format[3] == 1 ? '基站定位' : 'GPS定位'
        ];
        return implode(',', array_reverse($data));
    }

}
